home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / VideoStreamV1.0 / Source / mpegDecodeSrc / 24bit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  4.3 KB  |  177 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. #include "video.h"
  22. #include "dither.h"
  23.  
  24. /*
  25.  * We'll define the "ConvertColor" macro here to do fixed point arithmetic
  26.  * that'll convert from YCrCb to RGB using:
  27.  *    R = L + 1.40200*Cr;
  28.  *    G = L - 0.34414*Cb - 0.71414*Cr
  29.  *    B = L + 1.77200*Cb;
  30.  *
  31.  * We'll use fixed point by adding two extra bits after the decimal.
  32.  */
  33.  
  34. #define BITS    8
  35. #define ONE     ((int) 1)
  36. #define CONST_SCALE    (ONE << BITS)
  37. #define ROUND_FACTOR    (ONE << (BITS-1))
  38.  
  39. /* Macro to convert integer to fixed. */
  40. #define UP(x)    (((int)(x)) << BITS)
  41.  
  42. /* Macro to convert fixed to integer (with rounding). */
  43. #define DOWN(x)    (((x) + ROUND_FACTOR) >> BITS)
  44.  
  45. /* Macro to convert a float to a fixed */
  46. #define FIX(x)  ((int) ((x)*CONST_SCALE + 0.5))
  47.  
  48. #define CLAMP(ll,x,ul)    ( ((x)<(ll)) ?(ll):( ((x)>(ul)) ?(ul):(x)))
  49.  
  50. static int *Cb_r_tab, *Cr_g_tab, *Cb_g_tab, *Cr_b_tab;
  51.  
  52. /*
  53.  *--------------------------------------------------------------
  54.  *
  55.  * InitColorDither --
  56.  *
  57.  *    To get rid of the multiply and other conversions in color
  58.  *    dither, we use a lookup table.
  59.  *
  60.  * Results:
  61.  *    None.
  62.  *
  63.  * Side effects:
  64.  *    The lookup tables are initialized.
  65.  *
  66.  *--------------------------------------------------------------
  67.  */
  68.  
  69. void
  70. InitColorDither()
  71. {
  72.     int CR, CB, i;
  73.  
  74.     Cr_b_tab = (int *)malloc(256*sizeof(int));
  75.     Cr_g_tab = (int *)malloc(256*sizeof(int));
  76.     Cb_g_tab = (int *)malloc(256*sizeof(int));
  77.     Cb_r_tab = (int *)malloc(256*sizeof(int));
  78.  
  79.     for (i=0; i<256; i++) {
  80.     CB = CR = i;
  81.  
  82.     CB -= 128; CR -= 128;
  83.  
  84.     Cb_r_tab[i] = FIX(1.40200) * CB;
  85.     Cr_g_tab[i] = -FIX(0.34414) * CR;
  86.     Cb_g_tab[i] = -FIX(0.71414) * CB;   
  87.     Cr_b_tab[i] = FIX(1.77200) * CR;
  88.     }
  89. }
  90.  
  91.  
  92. /*
  93.  *--------------------------------------------------------------
  94.  *
  95.  * ColorDitherImage --
  96.  *
  97.  *    Converts image into 24 bit color.
  98.  *
  99.  * Results:
  100.  *    None.
  101.  *
  102.  * Side effects:
  103.  *    None.
  104.  *
  105.  *--------------------------------------------------------------
  106.  */
  107.  
  108. void
  109. ColorDitherImage(lum, cr, cb, out, rows, cols)
  110.   unsigned char *lum;
  111.   unsigned char *cr;
  112.   unsigned char *cb;
  113.   unsigned char *out;
  114.   int cols, rows;
  115.  
  116. {
  117.     int L, CR, CB;
  118.     unsigned char *row1, *row2;
  119.     unsigned char *lum2;
  120.     int x, y, colsT3;
  121.     unsigned int r, b, g;
  122.     int cb_r, cr_g, cb_g, cr_b, R, G, B;
  123.  
  124.     row1 = out;
  125.     colsT3 = 3 * cols;
  126.     row2 = row1 + colsT3;
  127.     lum2 = lum + cols;
  128.     for (y=0; y<rows; y+=2) {
  129.         for (x=0; x<cols; x+=2) {
  130.             cb_r = Cb_r_tab[*cb];
  131.             cr_g = Cr_g_tab[*cr];
  132.             cb_g = Cb_g_tab[*cb++];
  133.             cr_b = Cr_b_tab[*cr++];
  134.     
  135.             L = UP(*lum++);
  136.             R = (L + cb_r)>>BITS;
  137.             G = (L + cr_g + cb_g)>>BITS;
  138.             B = (L + cr_b)>>BITS;
  139.             *row1++ = CLAMP(0,R,255);
  140.             *row1++ = CLAMP(0,G,255);
  141.             *row1++ = CLAMP(0,B,255);
  142.     
  143.             L = UP(*lum++);
  144.             R = (L + cb_r)>>BITS;
  145.             G = (L + cr_g + cb_g)>>BITS;
  146.             B = (L + cr_b)>>BITS;
  147.             *row1++ = CLAMP(0,R,255);
  148.             *row1++ = CLAMP(0,G,255);
  149.             *row1++ = CLAMP(0,B,255);
  150.     
  151.             /* Now, do second row */
  152.             L = UP(*lum2++);
  153.             R = (L + cb_r)>>BITS;
  154.             G = (L + cr_g + cb_g)>>BITS;
  155.             B = (L + cr_b)>>BITS;
  156.             *row2++ = CLAMP(0,R,255);
  157.             *row2++ = CLAMP(0,G,255);
  158.             *row2++ = CLAMP(0,B,255);
  159.     
  160.             L = UP(*lum2++);
  161.             R = (L + cb_r)>>BITS;
  162.             G = (L + cr_g + cb_g)>>BITS;
  163.             B = (L + cr_b)>>BITS;
  164.             *row2++ = CLAMP(0,R,255);
  165.             *row2++ = CLAMP(0,G,255);
  166.             *row2++ = CLAMP(0,B,255);
  167.         }
  168.         lum += cols;
  169.         lum2 += cols;
  170.         row1 += colsT3;
  171.         row2 += colsT3;
  172.     }
  173. }
  174.  
  175.  
  176.  
  177.